package com.sunshine.lnuplus.view.TimetableSetting;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sunshine.lnuplus.utils.TimeHelper;

public class TimetableSettingViewModel extends AndroidViewModel {
    private SharedPreferences timetable_setting_sp;
    private SharedPreferences appSetting;
    private SharedPreferences.Editor editor;
    private MutableLiveData<Integer> weeksCount;
    private MutableLiveData<Integer> lessonCount;
    private MutableLiveData<Boolean> showWeekend;
    private MutableLiveData<Integer> heights;
    private MutableLiveData<Integer> buttonAlpha;
    private MutableLiveData<Integer> buttonRadius;
    private MutableLiveData<Float> lessonButtonTextSize;
    private MutableLiveData<Integer> buttonHeightDistance;
    private MutableLiveData<Integer> buttonWidthDistance;
    private MutableLiveData<Integer> buttonHeightPadding;
    private MutableLiveData<Integer> buttonWidthPadding;
    private MutableLiveData<Boolean> showNotInThisWeekLesson;
    private MutableLiveData<Boolean> showTeacher;
    private MutableLiveData<Boolean> showLessonTime;
    private MutableLiveData<Float> timeButtonTextSize;
    private MutableLiveData<Float> dateButtonTextSize;
    private MutableLiveData<Integer> beginDateMonth,beginDateDay;

    public TimetableSettingViewModel(@NonNull Application application) {
        super(application);

        weeksCount = new MutableLiveData<>();
        lessonCount = new MutableLiveData<>();
        showWeekend = new MutableLiveData<>();
        heights = new MutableLiveData<>();
        buttonAlpha = new MutableLiveData<>();
        buttonRadius = new MutableLiveData<>();
        lessonButtonTextSize = new MutableLiveData<>();
        buttonHeightDistance = new MutableLiveData<>();
        buttonWidthDistance = new MutableLiveData<>();
        buttonHeightPadding = new MutableLiveData<>();
        buttonWidthPadding = new MutableLiveData<>();
        showNotInThisWeekLesson = new MutableLiveData<>();
        showTeacher = new MutableLiveData<>();
        showLessonTime = new MutableLiveData<>();
        timeButtonTextSize = new MutableLiveData<>();
        dateButtonTextSize = new MutableLiveData<>();
        beginDateMonth = new MutableLiveData<>();
        beginDateDay = new MutableLiveData<>();

        timetable_setting_sp = getApplication().getSharedPreferences("timetable_setting",Context.MODE_PRIVATE);
        appSetting = getApplication().getSharedPreferences("app_setting",Context.MODE_PRIVATE);
        editor = timetable_setting_sp.edit();

        weeksCount.setValue(timetable_setting_sp.getInt("weeks_count",16));
        lessonCount.setValue(timetable_setting_sp.getInt("lesson_count",5));
        showWeekend.setValue(timetable_setting_sp.getBoolean("show_weekend",true));
        heights.setValue(timetable_setting_sp.getInt("heights",300));
        buttonAlpha.setValue(timetable_setting_sp.getInt("button_alpha", 255));
        buttonRadius.setValue(timetable_setting_sp.getInt("button_radius",20));
        lessonButtonTextSize.setValue(timetable_setting_sp.getFloat("lesson_button_text_size",11));
        buttonHeightDistance.setValue(timetable_setting_sp.getInt("button_height_distance",10));
        buttonWidthDistance.setValue(timetable_setting_sp.getInt("button_width_distance",10));
        buttonHeightPadding.setValue(timetable_setting_sp.getInt("button_height_padding",5));
        buttonWidthPadding.setValue(timetable_setting_sp.getInt("button_width_padding",5));
        showNotInThisWeekLesson.setValue(timetable_setting_sp.getBoolean("show_not_in_this_week_lesson",true));
        showTeacher.setValue(timetable_setting_sp.getBoolean("show_teacher",true));
        showLessonTime.setValue(timetable_setting_sp.getBoolean("show_lesson_time",true));
        timeButtonTextSize.setValue(timetable_setting_sp.getFloat("time_button_text_size",11));
        dateButtonTextSize.setValue(timetable_setting_sp.getFloat("date_button_text_size",12));
        if(TimeHelper.getMonth()>8) beginDateMonth.setValue(timetable_setting_sp.getInt("begin_date_month",8));
        else beginDateMonth.setValue(timetable_setting_sp.getInt("begin_date_month",2));
        beginDateDay.setValue(timetable_setting_sp.getInt("begin_date_day",1));
    }

    public void toSave(Handler handler){
        editor.putInt("weeks_count",weeksCount.getValue());
        if(weeksCount.getValue()<timetable_setting_sp.getInt("this_week",1)){
            editor.putInt("this_week",weeksCount.getValue());
        }
        editor.putInt("lesson_count",lessonCount.getValue());
        editor.putBoolean("show_weekend",showWeekend.getValue());
        editor.putInt("heights",heights.getValue());
        editor.putInt("button_alpha",buttonAlpha.getValue());
        editor.putInt("button_radius",buttonRadius.getValue());
        editor.putFloat("lesson_button_text_size",lessonButtonTextSize.getValue());
        editor.putInt("button_height_distance",buttonHeightDistance.getValue());
        editor.putInt("button_width_distance",buttonWidthDistance.getValue());
        editor.putInt("button_height_padding",buttonHeightPadding.getValue());
        editor.putInt("button_width_padding",buttonWidthPadding.getValue());
        editor.putBoolean("show_not_in_this_week_lesson",showNotInThisWeekLesson.getValue());
        editor.putBoolean("show_teacher",showTeacher.getValue());
        editor.putBoolean("show_lesson_time",showLessonTime.getValue());
        editor.putFloat("time_button_text_size",timeButtonTextSize.getValue());
        editor.putFloat("date_button_text_size",dateButtonTextSize.getValue());
        editor.putInt("begin_date_month",beginDateMonth.getValue());
        editor.putInt("begin_date_day",beginDateDay.getValue());
        editor.apply();
        handler.sendEmptyMessage(1);
    }

    public void toDefault(){
        editor.clear();
        editor.apply();
    }

    public MutableLiveData<Integer> getWeeksCount() {
        return weeksCount;
    }

    public MutableLiveData<Boolean> getShowWeekend() {
        return showWeekend;
    }

    public MutableLiveData<Integer> getHeights() {
        return heights;
    }

    public MutableLiveData<Integer> getButtonAlpha() {
        return buttonAlpha;
    }

    public MutableLiveData<Integer> getButtonRadius() {
        return buttonRadius;
    }

    public MutableLiveData<Integer> getButtonHeightDistance() {
        return buttonHeightDistance;
    }

    public MutableLiveData<Integer> getLessonCount() {
        return lessonCount;
    }

    public MutableLiveData<Float> getLessonButtonTextSize() {
        return lessonButtonTextSize;
    }

    public MutableLiveData<Integer> getButtonWidthDistance() {
        return buttonWidthDistance;
    }

    public MutableLiveData<Integer> getButtonHeightPadding() {
        return buttonHeightPadding;
    }

    public MutableLiveData<Integer> getButtonWidthPadding() {
        return buttonWidthPadding;
    }

    public MutableLiveData<Boolean> getShowNotInThisWeekLesson() {
        return showNotInThisWeekLesson;
    }

    public MutableLiveData<Boolean> getShowTeacher() {
        return showTeacher;
    }

    public MutableLiveData<Float> getDateButtonTextSize() {
        return dateButtonTextSize;
    }

    public MutableLiveData<Boolean> getShowLessonTime() {
        return showLessonTime;
    }

    public MutableLiveData<Float> getTimeButtonTextSize() {
        return timeButtonTextSize;
    }

    public MutableLiveData<Integer> getBeginDateMonth() {
        return beginDateMonth;
    }

    public MutableLiveData<Integer> getBeginDateDay() {
        return beginDateDay;
    }

    public boolean isEverydayBackground() {
        return appSetting.getBoolean("everyday_background",true);
    }
}
